//
//  DetailsViewController.m
//  Travel
//
//  Created by lanou3g on 15/12/9.
//  Copyright © 2015年 涂晓雨. All rights reserved.
//

#import "DetailsViewController.h"
#import "CX_DataDownloadTools.h"
#import "DetailsModel.h"

@interface DetailsViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation DetailsViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"听说";
    //滑动隐藏导航栏
//    self.navigationController.hidesBarsOnSwipe = YES;
    //设置左barbutton
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont.png"] style:UIBarButtonItemStyleDone target:self action:@selector(actionLeft:)];
    //添加webView
    self.DetailsWebView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    self.DetailsWebView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.DetailsWebView];
    self.DetailsWebView.delegate = self;
    

    
    //设置字体图片大小适应屏幕
//    self.DetailsWebView.scalesPageToFit = YES;
    
    self.DetailsWebView.opaque = NO;

    NSString *str = [NSString stringWithFormat:@"http://webapi.yilule.com:5580//api/Listen?listenId=%@",self.model.Id];
    
        [CX_DataDownloadTools downloadDataWithURL:str andMethod:@"GET" andBody:nil andBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
          NSDictionary *dict = dic[@"Data"];
        NSDictionary *dics = dict[@"ReviewList"];
        NSArray *arr = dics[@"Reviews"];
        for (NSDictionary *dicss in arr) {
            DetailsModel *m = [[DetailsModel alloc]init];
            [m setValuesForKeysWithDictionary:dicss];
            [self.dataArray addObject:m];
//            NSLog(@"%@--%@",dict,arr);
        }
//        NSString *imaHTML = @"<div   align = center ><img src = \"http://file0.yilule.com/attached/image/20151120/20151120174135_0570.jpg\" /  width = \"300\" height = \"%220\"></div>";
        
        NSString *str = dict[@"Content"];
        
//          NSString *str2 = [str stringByReplacingOccurrencesOfString:@"<img alt=\"\" src=\"http://file0.yilule.com/attached/image/20151120/20151120174135_0570.jpg\" />" withString:imaHTML];
        
        [self.DetailsWebView loadHTMLString:str baseURL:nil];
        
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //改变webView中的字体大小
   NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
   [self.DetailsWebView stringByEvaluatingJavaScriptFromString:str];
    //改变图片的大小
    [self.DetailsWebView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=300;"//缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
      "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth) * 1.5;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [self.DetailsWebView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];

}

//左barbutton监听
- (void)actionLeft:(UIBarButtonItem *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
